﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using Debug = UnityEngine.Debug;

namespace UFrame
{
    public static class SVN
    {
        private static List<string> drives = new List<string>() { "c:", "d:", "e:", "f:" };
        private static string svnPath = @"\Program Files\TortoiseSVN\bin\";
        private static string svnProc = @"TortoiseProc.exe";
        private static string svnProcPath = "";
        private static string svnHelperPath = @"\Plugins\SvnHelper.bat";

        [MenuItem("Assets/SVN/Log")]
        public static void Log()
        {
            string path = GetActiveObjectPath();
            if (File.Exists(path))
            {
                svnCommand("log", path);
            }
            else if (Directory.Exists(path))
            {
                svnCommand("log", path);
            }
        }

        [MenuItem("Assets/SVN/Up")]
        public static void Up()
        {
            string path = GetActiveObjectPath();
            if (File.Exists(path))
            {
                svnUp(path);
            }
            else if (Directory.Exists(path))
            {
                svnUp(path);
            }
        }

        [MenuItem("Assets/SVN/Commit")]
        public static void Commit()
        {
            string path = GetActiveObjectPath();

            if (File.Exists(path))
            {
                svnCo(path);
            }
            else if (Directory.Exists(path))
            {
                svnCo(path);
            }
        }

        [MenuItem("Assets/SVN/Cleanup")]
        public static void Cleanup()
        {
            string path = GetActiveObjectPath();

            if (File.Exists(path))
            {
                svnCleanup(path);
            }
            else if (Directory.Exists(path))
            {
                svnCleanup(path);
            }
        }

        [MenuItem("Assets/SVN/Revert")]
        public static void Revert()
        {
            string path = GetActiveObjectPath();

            if (File.Exists(path))
            {
                svnRevert(path);
            }
            else if (Directory.Exists(path))
            {
                svnRevert(path);
            }
        }

        public static string GetActiveObjectPath()
        {
            var select = Selection.activeObject;

            if (select != null)
            {
                string path = AssetDatabase.GetAssetPath(select);
                string foldPath = Directory.GetCurrentDirectory();
                path = foldPath + "/" + path;
                return path;
            }
            else
            {
                Debug.Log("没有在unity 资源管理器中选中对象");
                return "";
            }
        }

        public static void svnUp(string path)
        {
            if (string.IsNullOrEmpty(svnProcPath))
            {
                svnProcPath = GetSvnProcPath();
            }

            string para = "/command:update /path:" + path + " /closeonend:0";
            Process.Start(svnProc, para);
        }

        public static void svnCo(string path)
        {
            if (string.IsNullOrEmpty(svnProcPath))
            {
                svnProcPath = GetSvnProcPath();
            }

            string para = "/command:commit /path:" + path + " /closeonend:0";
            Process.Start(svnProc, para);
        }

        public static void svnCleanup(string path)
        {
            if (string.IsNullOrEmpty(svnProcPath))
            {
                svnProcPath = GetSvnProcPath();
            }

            string para = "/command:cleanup /path:" + path + " /closeonend:0";
            Process.Start(svnProc, para);
        }

        public static void svnLog(string path)
        {
            if (string.IsNullOrEmpty(svnProcPath))
            {
                svnProcPath = GetSvnProcPath();
            }

            string para = "/command:log /path:" + path + " /closeonend:0";
            Process.Start(svnProc, para);
        }

        public static void svnRevert(string path)
        {
            if (string.IsNullOrEmpty(svnProcPath))
            {
                svnProcPath = GetSvnProcPath();
            }

            string para = "/command:revert /path:" + path + " /closeonend:0";
            Process.Start(svnProc, para);
        }

        private static void svnCommand(string command, string path)
        {
            if (string.IsNullOrEmpty(svnProcPath))
            {
                svnProcPath = GetSvnProcPath();
            }

            string para = "/command:" + command + " /path:\"" + path + "\" /closeonend:0";
            Process.Start(svnProcPath, para);
        }

        public static string GetSvnProcPath()
        {
            foreach (var item in drives)
            {
                var path = string.Concat(item, svnPath, svnProc);
                if (File.Exists(path))
                {
                    return path;
                }
            }

            return EditorUtility.OpenFilePanel("Select TortoiseProc.exe", "c:\\", "exe");
        }

        static public void RunSvnHelper(string param1, string param2)
        {
            Process.Start(svnHelperPath, param1 + " " + param2);
        }
    }
}

